﻿using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using System.Data.SqlClient;

using Jungle.Prodamo.AdvertManager;
using Jungle.Prodamo.MostNeedFunctions;
using Jungle.Prodamo.Core;

public partial class admin : BaseAdminModule
{
    public string backUrl;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack) Session.Contents["backUrl"] = Request.UrlReferrer.AbsoluteUri;
        UserManager um = new UserManager();
        um.GetAll();
        List<UserManager> Users = um.UserList;
        foreach (UserManager user in Users)
        {
            ddl_users.Items.Add(new ListItem(user.NickName, user.UserId.ToString()));
        }
        LoadData();

        switch (Request["cmd"])
        {
            case "Add":
                {
                    AdvertMenu.ActiveViewIndex = 1;
                    break;
                }
            case "byrubrik":
                {
                    AdvertMenu.ActiveViewIndex = 2;
                    ByRubrik1.RubrikId = Convert.ToInt32(Request["rId"]);
                    break;
                }
            case "delete":
                {
                    delForm.Visible = true;
                    AdvertMenu.ActiveViewIndex = 0;
                    break;
                }
            case "noFav":
                {
                    Advert adv = new Advert();
                    adv.AdvertId = Convert.ToInt32(Request["aId"]);
                    adv.GetById();
                    adv.PremiumType = false;
                    adv.Update();
                    Response.Redirect(Session.Contents["backUrl"].ToString());
                    break;
                }
            case "addFav":
                {
                    Advert adv = new Advert();
                    adv.AdvertId = Convert.ToInt32(Request["aId"]);
                    adv.GetById();
                    adv.PremiumType = true;
                    adv.Update();
                    Response.Redirect(Session.Contents["backUrl"].ToString());
                    break;
                }
            default: 
                {
                    AdvertMenu.ActiveViewIndex = 0;
                    break;
                }
        }

    }
    protected void Page_Init(object sender, EventArgs e)
    {

    }
    public override void FillBaseInfo()
    {
        sysName = "Adverts";
    }
    public override void Main()
    {
    }
    protected void LoadData()
    {
        Rubrik rbs = new Rubrik();
        rbs.GetAll();
        foreach (Rubrik rb in rbs.RubrikList)
        {
            if (rb.ParentId == 0)
                ddl_Rubrik.Items.Add(new ListItem(rb.Title, rb.RubrikId.ToString()));

            Rubrik childs = new Rubrik();
            childs.ParentId = rb.RubrikId;
            childs.GetByParentId();

            if (childs.RubrikList.Count > 0)
                foreach (Rubrik child in childs.RubrikList)
                {
                    ddl_Rubrik.Items.Add(new ListItem(" --- " + child.Title, child.RubrikId.ToString()));
                }
        }
    }
    protected void AddAdvert(object sender, EventArgs e)
    {
        ValidBlock.InnerHtml = "";
        bool valid = true;
        if (tb_Title.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите заголовок <br/>"; }
        if (tb_Price.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите цену <br/>"; }
        if (tb_Body.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Опишите хоть немного ваше объявление<br/>"; }

        if (valid)
        {
            Advert adv = new Advert();
            adv.UserId = Convert.ToInt32(ddl_users.SelectedValue);
            adv.Title = tb_Title.Text.Trim();
            adv.Type = Convert.ToInt32(ddl_Type.SelectedValue);
            adv.Body = tb_Body.Text.Trim();
            adv.EndDate = DateTime.Now.AddDays(Convert.ToInt32(ddl_Time.SelectedValue));
            adv.Price = Convert.ToDouble(tb_Price.Text.Trim());
            adv.Currency = Convert.ToInt32(ddl_Currency.SelectedValue);
            adv.PremiumType = true;
            if (!fu_Image.HasFile) adv.ImageUrl = "NONE";
            else
            {
                fu_Image.PostedFile.SaveAs(Server.MapPath("~/Images/AdvertsImg/" + fu_Image.FileName));
                adv.ImageUrl = "~/Images/AdvertsImg/" + fu_Image.FileName;
            }
            adv.Add();

            ABR abr = new ABR();
            abr.RubrikId = Convert.ToInt32(ddl_Rubrik.SelectedValue);
            abr.AdvertId = adv.AdvertId;
            abr.Add();

            UserManager um = new UserManager();
            um.UserId = Convert.ToInt32(ddl_users.SelectedValue);
            um.GetById();
        }
        else
            ValidBlock.InnerHtml += "Вы ввели неверный код подтверждения";
    }

    public void yesDel(object sender, EventArgs e)
    {
        if (Request["aId"] != null)
        {
            Advert adv = new Advert();
            adv.AdvertId = Convert.ToInt32(Request["aId"]);
            adv.Delete();
        }
        Response.Redirect(Session.Contents["backUrl"].ToString());
    }
    public void noDel(object sender, EventArgs e)
    {
        Response.Redirect(Session.Contents["backUrl"].ToString());
    }
}
